package com.ruoyi.system.mapper;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.domain.CsInventory;
import com.ruoyi.system.domain.CsInventoryVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 * 库存物品Mapper接口
 *
 * @author ruoyi
 * @date 2025-10-05
 */
@Repository
public interface CsInventoryMapper extends BaseMapper<CsInventory> {
    /**
     * 查询库存物品
     *
     * @param id 库存物品主键
     * @return 库存物品
     */
    public CsInventory selectCsInventoryById(Long id);

    /**
     * 查询库存物品列表
     *
     * @param csInventory 库存物品
     * @return 库存物品集合
     */
    public List<CsInventoryVo> selectCsInventoryList(CsInventory csInventory);

    /**
     * 新增库存物品
     *
     * @param csInventory 库存物品
     * @return 结果
     */
    public int insertCsInventory(CsInventory csInventory);

    /**
     * 修改库存物品
     *
     * @param csInventory 库存物品
     * @return 结果
     */
    public int updateCsInventory(CsInventory csInventory);

    /**
     * 删除库存物品
     *
     * @param id 库存物品主键
     * @return 结果
     */
    public int deleteCsInventoryById(Long id);

    /**
     * 批量删除库存物品
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteCsInventoryByIds(String[] ids);

    @Insert("INSERT INTO cs_inventory " +
            "(user_id, account, name, market_hash_name, quantity, unit_cost, available_quantity, buff_price, steam_price, uu_price, history_profit) " +
            "VALUES (#{userId}, #{account}, #{name}, #{marketHashName}, #{quantity}, #{unitCost}, #{availableQuantity}, #{buffPrice}, #{steamPrice}, " +
            "#{uuPrice}, #{historyProfit}) " +
            "ON DUPLICATE KEY UPDATE " +
            "quantity = VALUES(quantity), " +
            "unit_cost = VALUES(unit_cost)")
    int upsetInventory(CsInventory csInventory);

    @Update("<script>" +
            "<foreach collection='list' item='item' separator=';'>" +
            "UPDATE cs_inventory " +
            "SET steam_price = #{item.steamPrice}, " +
            "    uu_price = #{item.uuPrice}, " +
            "    buff_price = #{item.buffPrice}, " +
            "    c5_price = #{item.c5Price}, update_date = #{item.updateDate} " +
            "WHERE market_hash_name = #{item.marketHashName}" +
            "</foreach>" +
            "</script>")
    int batchUpdatePlatformPrice(List<CsInventory> inventories);


    @Select("SELECT *," +
            "       CASE WHEN unit_cost != 0 THEN unit_cost * quantity ELSE 0 END AS unit_cost_total, " +
            "       CASE WHEN steam_price != 0 THEN (steam_price * 0.8) ELSE 0 END AS steam_balance_price, " +
            "       CASE WHEN uu_price != 0 and steam_price != 0 THEN ROUND((uu_price / (steam_price * 0.87) * 100) * 100,4) ELSE 0 END AS steam_balance_discount_by_uu, " +
            "       CASE WHEN unit_cost != 0 and steam_price != 0 THEN ROUND((unit_cost / (steam_price * 0.87) * 100) * 100,4) ELSE 0 END AS steam_balance_discount, " +
            "       CASE WHEN unit_cost != 0 and uu_price != 0 THEN (unit_cost - uu_price) ELSE 0 END AS uu_profit, " +
            "       CASE WHEN unit_cost != 0 and c5_price != 0 THEN (unit_cost - c5_price) ELSE 0 END AS c5_profit, " +
            "       CASE WHEN unit_cost != 0 and buff_price != 0 THEN (unit_cost - buff_price) ELSE 0 END AS buff_profit, " +
            "       CASE WHEN unit_cost != 0 and uu_price != 0 THEN (unit_cost - uu_price) * quantity ELSE 0 END AS uu_total_profit, " +
            "       CASE WHEN unit_cost != 0 and c5_price != 0 THEN (unit_cost - c5_price) * quantity ELSE 0 END AS c5_total_profit, " +
            "       CASE WHEN unit_cost != 0 and buff_price != 0 THEN (unit_cost - buff_price) * quantity ELSE 0 END AS buff_total_profit " +
            "FROM cs_inventory ${ew.customSqlSegment}")
    List<CsInventoryVo> selectCsInventoryVoList(@Param("ew") Wrapper<CsInventory> wrapper);
}
